package com.hbue.springboot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbue.springboot.pojo.DTO.TbAccountDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hbue.springboot.mapper.TbAccountMapper;
import com.hbue.springboot.pojo.TbAccount;
import com.hbue.springboot.service.TbAccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Objects;

/**
 * 功能：
 * 日期：2025/1/12 16:20
 */
@Service
@RequiredArgsConstructor
public class TbAccountServiceImpl extends ServiceImpl<TbAccountMapper, TbAccount> implements TbAccountService {

    private final TbAccountMapper tbAccountMapper;

    private static final Logger log = LoggerFactory.getLogger(TbAccountServiceImpl.class);

    /**
     * 用户登录
     * 步骤：
     * 1 根据用户名查询对应的账户信息
     * 2 判断账户是否为空
     * 3 条件成立：表示账户为空，返回false，登录失败
     * 4 条件不成立：将前端采集的密码进行MD5加密，生成密文密码
     * 5 将数据库的密码和密文密码进行比较，两者相同登录成功，否则登录失败
     *
     * @param userName 前端采集的用户名
     * @param userPwd  前端采集的密码
     * @return true 登录成功， false 登录失败
     */
    @Override
    public TbAccountDTO login(String userName, String userPwd) {
        LambdaQueryWrapper<TbAccount> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbAccount::getUserName, userName);
        TbAccount account = tbAccountMapper.selectOne(wrapper);
        if (null == account) {
            log.info("用户名不存在");
            return null;
        }
        String encodingPwd = DigestUtils.md5DigestAsHex(userPwd.getBytes());
        log.info(encodingPwd);
        boolean res = Objects.equals(encodingPwd, account.getUserPwd());
        if (res) {
            log.info("登录成功");
            return new TbAccountDTO(account);
        } else {
            log.info("账号或密码错误");
            return null;
        }
    }

    /**
     * 修改登录密码
     * 步骤：
     * 1 获取前端采集的用户名和密码
     * 2 对密码进行MD5加密并生成密文密码
     * 3 调用AccountMapper对象的modifyUserPwd方法修改密码
     * @param tbAccountDTO 前端采集的账户信息
     * @return true 修改密码成功，false修改密码失败
     */
    @Override
    public int modifyUserPwd(TbAccountDTO tbAccountDTO) {
        String userName = tbAccountDTO.getUserName();
        String userPwd = tbAccountDTO.getUserPwd();
        String oldPwd = tbAccountDTO.getOldPwd();
        // 前端采集的新密码、原密码进行加密
        String encryptPwd = DigestUtil.md5Hex(userPwd);
        String encryptOldPwd = DigestUtil.md5Hex(oldPwd);
        LambdaQueryWrapper<TbAccount> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbAccount::getUserName, userName);
        TbAccount account = tbAccountMapper.selectOne(wrapper);
        if (Objects.equals(account.getUserPwd(), encryptOldPwd)){
            if(tbAccountMapper.modifyUserPwd(userName, encryptPwd) > 0){
                return 1;
            }
            else{
                return 0;
            }
        }else{
            return -1;
        }

    }

    /**
     * 获取个人信息
     * @param id
     * @return
     */
    @Override
    public TbAccountDTO getUserById(Long id) {
        TbAccount tbAccount = tbAccountMapper.selectById(id);
        TbAccountDTO tbAccountDTO = BeanUtil.copyProperties(tbAccount, TbAccountDTO.class);
        return tbAccountDTO;
    }

    @Override
    public int updateUserInfo(Integer id, TbAccountDTO accountDTO) {
        TbAccount tbAccount = new TbAccount();
        tbAccount.setId(id);
        tbAccount.setUserEmail(accountDTO.getUserEmail());
        tbAccount.setUserMobile(accountDTO.getUserMobile());
        return tbAccountMapper.updateById(tbAccount);
    }

}
